<?php 
	
function doPages($paramName, $page_size, $thepage, $query_string='', $total=0) {
// 	$paramName: tên tham số để lấy trang hiện tại
//	$page_size: số row/trang
//	$thepage: URL của thẻ a
//	$query_string: tham số truyền vô thêm URL  
//	$total: tổng số row của data
  
  //số trang sẽ hiển thị cùng với Đầu Trước . . . . Sau Cuối
    $index_limit = 7;

	$query='';

    if(strlen($query_string)>0){
        $query = "&".$query_string;
    }

    //get the current page number example: 3, 4 etc: see above method description
    $current = get_current_page($paramName);

    $total_pages=ceil($total/$page_size);
    $start=max($current-intval($index_limit/2), 1); 
    $end=$start+$index_limit-1; 
	
	
	echo '<div align="center" class="paging">';

   
    if($start > 1) {
        $i = 1;
        echo "<a title='go to page {$i}' href='{$thepage}?{$paramName}={$i}$query'>Đầu</a>&nbsp;";
    }

	 if($current==1) {
        echo "&nbsp;&nbsp;<span class='prn'>«</span> &nbsp;&nbsp;";
    } else {
        $i = $current-1;
        echo "&nbsp;&nbsp;<a class='prn' title='go to page {$i}' rel='nofollow' href='{$thepage}?{$paramName}={$i}$query'> «</a>&nbsp;&nbsp;";
        echo "&nbsp;<span class='prn'>...</span>&nbsp;";
    }

	
    for ($i = $start; $i <= $end && $i <= $total_pages; $i++){
        if($i==$current) {
            echo "&nbsp;<span> $i</span>&nbsp;";
        } else {
            echo "&nbsp;<a title='go to page {$i}' href='{$thepage}?{$paramName}={$i}$query'>&nbsp;{$i}&nbsp;</a>";
        }
    }

    if($current < $total_pages) {
        $i = $current+1;
        echo "&nbsp;<span class='prn'>...</span>&nbsp;";
        echo "&nbsp;&nbsp;<a class='prn' title='go to page {$i}' rel='nofollow' href='{$thepage}?{$paramName}={$i}$query'>»</a>&nbsp;&nbsp;";
    } else {
        echo "<span class='prn'>&nbsp;&nbsp;»&nbsp;&nbsp;</span> ";
    }
	
	 if($total_pages > $end){
        $i = $total_pages;
        echo "&nbsp;<a title='go to page {$i}' href='{$thepage}?{$paramName}={$i}$query'>Cuối</a>&nbsp;";
    }
    //if nothing passed to method or zero, then dont print result, else print the total count below:
    if ($total != 0){
        //prints the total result count just below the paging
        echo "(Trang $current/$total_pages - Total: $total)</div>";
    }
	
}//end of method doPages()

//Both of the functions below required

function check_integer($which) {
    if(isset($_REQUEST[$which])){
        if (intval($_REQUEST[$which])>0) {
            //check the paging variable was set or not,
            //if yes then return its number:
            //for example: ?{$paramName}=5, then it will return 5 (integer)
            return intval($_REQUEST[$which]);
        } else {
            return false;
        }
    }
    return false;
}//end of check_integer()

function get_current_page($paramName) {
    if(($var=check_integer($paramName))) {
        //return value of 'page', in support to above method
        return $var;
    } else {
        //return 1, if it wasnt set before, page=1
        return 1;
    }
}//end of method get_current_page()
?>